package cn.vivi.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.vivi.domain.Product;
import cn.vivi.domain.vo.QueryData;
import cn.vivi.service.ProductService;
import cn.vivi.service.impl.ProductServiceImpl;
import cn.vivi.util.JsonUtils;
import cn.vivi.util.Result;

@WebServlet("/products")
@SuppressWarnings("all")
public class ProductServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private ProductService productService = new ProductServiceImpl();

	private void initParamer(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf8");
		initParamer(request, response);
		String method = request.getParameter("method");
		if ("findAll".equals(method)) {
			this.findAll();
			return;
		}
		if ("delete".equals(method)) {
			this.deleteProduct();
			return;
		}
		if ("add".equals(method)) {
			this.addProduct();
			return;
		}
		if ("update".equals(method)) {
			this.updateProduct();
			return;
		}
		if ("queryData".equals(method)) {
			this.queryData();
		}
	}

	private void queryData() {
		QueryData data = new QueryData();
		data.setClassesId(request.getParameter("classesId"));
		if (!Objects.isNull(request.getParameter("minPrice"))) {
			data.setMinPrice(Double.parseDouble(request.getParameter("minPrice")));
		}
		if (!Objects.isNull(request.getParameter("maxPrice"))) {
			data.setMaxPrice(Double.parseDouble(request.getParameter("maxPrice")));
		}
		List<Product> products = productService.findByQueryData(data);
		JsonUtils.toJson(response, Result.success().createMap().setData("items", products));
	}

	private void updateProduct() {
		Product product = getProductInfo();
		product.setId(Integer.parseInt(request.getParameter("id")));
		if (productService.updateProduct(product)) {
			JsonUtils.toJson(response, Result.success().setMessage("更新商品信息成功"));
		} else {
			JsonUtils.toJson(response, Result.error().setMessage("更新商品信息失败"));
		}
	}

	private void addProduct() {
		Product product = getProductInfo();
		if (productService.addProduct(product)) {
			JsonUtils.toJson(response, Result.success().setMessage("添加商品成功"));
		} else {
			JsonUtils.toJson(response, Result.error().setMessage("添加商品失败"));
		}
	}

	private Product getProductInfo() {
		Product product = new Product();
		product.setName(request.getParameter("name"));
		product.setPrice(Double.parseDouble(request.getParameter("price")));
		product.setDescription(request.getParameter("description"));
		product.setClassesId(Integer.parseInt(request.getParameter("classesId")));
		product.setPicture(request.getParameter("picture"));
		return product;
	}

	private void deleteProduct() {
		String id = request.getParameter("id");
		if (productService.delete(id)) {
			JsonUtils.toJson(response, Result.success().setMessage("删除商品成功"));
		} else {
			JsonUtils.toJson(response, Result.error().setMessage("删除商品失败"));
		}
	}

	private void findAll() {
		List<Product> products = productService.findAll();
		JsonUtils.toJson(response, Result.success().createMap().setData("items", products));
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf8");
		StringBuffer data = new StringBuffer();
		String line = null;
		BufferedReader reader = null;
		reader = request.getReader();
		while (null != (line = reader.readLine())) {
			data.append(line);
		}
		Product product = (Product) JsonUtils.toObject(data.toString(), Product.class);
		if (productService.updateProduct(product)) {
			JsonUtils.toJson(response, Result.success().setMessage("更新商品信息成功"));
		} else {
			JsonUtils.toJson(response, Result.error().setMessage("更新商品信息失败"));
		}
	}

}
